var express = require('express');
const Result = require('../lib/Result');
const isRule = require('../util/isRule');
const Coupon = require('../db/models/coupon/coupon');
var router = express.Router();
/**
 * 增加优惠劵,159,157,6
 */
router.post("",(req,res)=>{
    let token = req.auth
    if (isRule(token.rule, [159, 157, 6], res, token.super)) {
        Coupon.addCoupon(req.body).then(value=>{
            res.json(Result.success(value.toJSON()))
        })
    }
})
/**
 * 删除优惠劵,181,157,6
 */
router.post("/:id/delete",(req,res)=>{
    let token = req.auth
    if (isRule(token.rule, [181, 157, 6], res, token.super)) {
        Coupon.delete(req.params.id).then(value=>{
            res.json(Result.success(true))
        })
    }
})
/**
 * 修改优惠劵状态,161,157,6
 */
router.post("/:id/update_status",(req,res)=>{
    let token = req.auth
    if (isRule(token.rule, [181, 157, 6], res, token.super)) {
        Coupon.updateCoupon(req.body,req.params.id).then(value=>{
            res.json(Result.success(true))
        })
    }
})
/**
 * 修改优惠劵,160,157,6
 */
router.post("/:id",(req,res)=>{
    let token = req.auth
    if (isRule(token.rule, [160, 157, 6], res, token.super)) {
        Coupon.updateCoupon(req.body,req.params.id).then(value=>{
            res.json(Result.success(true))
        })
    }
})
/**
 * 优惠劵列表,160,157,6
 */
router.get("/:page",(req,res)=>{
    let token = req.auth
    if (isRule(token.rule, [158, 157, 6], res, token.super)) {
        let page=req.params.page
        let limit=req.query.limit
        Coupon.CouponList(page,limit).then(value=>{
            res.json(Result.success(value))
        })
    }
})
module.exports=router